/*
* @Author: dyiwen
* @Date:   2020-09-17 17:08:54
* @Last Modified by:   dyiwen
* @Last Modified time: 2020-09-21 20:12:33
 */
package main

import (
	// "fmt"
	"github.com/urfave/cli"
	"os"
	"sort"
)

func init_cmd() {
	app := cli.NewApp()
	app.Name = "nacosctl"
	app.HelpName = "nacosctl"
	app.Usage = "运维使用的nacos管理工具"
	app.Version = "1.0.0"
	app.Author = "duanyiwen"
	// app.Flags = []cli.Flag{
	// 	cli.StringFlag{
	// 		Name:  "namespace, n",
	// 		Usage: "指定命名空间",
	// 	},
	// }

	app.Commands = []cli.Command{
		{
			Name: "check",
			// Aliases:  []string{"a"},
			Usage:       "自检",
			Description: "自检当前的基本信息",
			Category:    "Basic Commands (Intermediate)",
			Action:      checkHandler,
		},
		{
			Name: "get",
			// Aliases:  []string{"a"},
			Usage:    "查询",
			Category: "Basic Commands (Intermediate)",
			// Action:   getHandler,
			Subcommands: []cli.Command{
				{
					Name:    "dataid",
					Aliases: []string{"id"},
					Usage:   "查询dataid",
					Action:  getDataidHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:     "namespace, n",
							Usage:    "指定命名空间",
							Required: true,
						},
						cli.StringFlag{
							Name:  "group, g",
							Value: "DEFAULT_GROUP",
							Usage: "group",
						},
						cli.StringFlag{
							Name:  "G",
							Value: "DEFAULT_GROUP",
							Usage: "指定组名，始终大写",
						},
						cli.IntFlag{
							Name:  "page, i",
							Value: 1,
							Usage: "page",
						},
						cli.IntFlag{
							Name:  "pagesize, s",
							Value: 20,
							Usage: "pagesize",
						},
					},
				},
				{
					Name:    "namespace",
					Aliases: []string{"ns"},
					Usage:   "查询namespace",
					Action:  getNamespaceHandler,
				},
			},
		},
		{
			Name:     "describe",
			Usage:    "描述",
			Category: "Basic Commands (Intermediate)",
			Action:   describeHandler,
			Subcommands: []cli.Command{
				{
					Name:    "dataid",
					Aliases: []string{"id"},
					Usage:   "查询dataid",
					Action:  describeDataidHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:  "namespace, n",
							Usage: "namespace",
						},
						cli.StringFlag{
							Name:  "group, g",
							Usage: "group (default: DEFAULT_GROUP)",
						},
						cli.StringFlag{
							Name:  "G",
							Value: "DEFAULT_GROUP",
							Usage: "指定组名，始终大写",
						},
					},
				},
			},
		},
		{
			Name: "publish",
			// Aliases:  []string{"a"},
			Usage:    "发布",
			Category: "Basic Commands (Intermediate)",
			// Action:   publishHandler,
			Subcommands: []cli.Command{
				{
					Name:    "dataid",
					Aliases: []string{"id"},
					Usage:   "查询dataid",
					Action:  publishDataidHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:     "namespace, n",
							Usage:    "指定命名空间",
							Required: true,
						},
					},
				},
			},
		},
		{
			Name: "delete",
			// Aliases:  []string{"a"},
			Usage:    "删除",
			Category: "Basic Commands (Intermediate)",
			// Action:   deleteHandler,
			Subcommands: []cli.Command{
				{
					Name:    "dataid",
					Aliases: []string{"id"},
					Usage:   "查询dataid",
					Action:  deleteDataidHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:     "namespace, n",
							Usage:    "指定命名空间",
							Required: true,
						},
						cli.StringFlag{
							Name:  "group, g",
							Value: "DEFAULT_GROUP",
							Usage: "指定组名",
						},
						cli.StringFlag{
							Name:  "G",
							Value: "DEFAULT_GROUP",
							Usage: "指定组名，始终大写",
						},
					},
				},
			},
		},
		{
			Name: "import",
			// Aliases:  []string{"a"},
			Usage:    "导入",
			Category: "Basic Commands (Intermediate)",
			// Action:      importHandler,
			Subcommands: []cli.Command{
				{
					Aliases:     []string{"id"},
					Name:        "dataid",
					Usage:       "指定dataid",
					Description: "当前版本仅支持properties格式的键值对配置内容,其他诸如yaml，json将在后续版本中兼容",
					Action:      importHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:  "file, f",
							Usage: "指定文件",
						},
						cli.StringFlag{
							Name:     "namespace, n",
							Required: true,
							Usage:    "指定命名空间",
						},
						cli.StringFlag{
							Name:  "group, g",
							Value: "DEFAULT_GROUP",
							Usage: "指定组名",
						},
						cli.StringFlag{
							Name:  "G",
							Value: "DEFAULT_GROUP",
							Usage: "指定组名，始终大写",
						},
					},
				},
			},
		},
		{
			Name: "export",
			// Aliases:  []string{"a"},
			Usage:    "导出",
			Category: "Basic Commands (Intermediate)",
			// Action:   exportHandler,
			Subcommands: []cli.Command{
				{
					Aliases: []string{"id"},
					Name:    "dataid",
					Usage:   "指定dataid",
					Action:  exportHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:  "file, f",
							Usage: "指定文件",
						},
						cli.StringFlag{
							Name:     "namespace, n",
							Required: true,
							Usage:    "指定命名空间",
						},
						cli.StringFlag{
							Name:  "group, g",
							Value: "DEFAULT_GROUP",
							Usage: "指定组名",
						},
						cli.StringFlag{
							Name:  "G",
							Value: "DEFAULT_GROUP",
							Usage: "指定组名，始终大写",
						},
						cli.StringFlag{
							Name:  "output, o",
							Value: "./nacos_config_export.ini",
							Usage: "指定导出的文件路径",
						},
					},
				},
			},
		},
		{
			Name: "login",
			// Aliases:  []string{"a"},
			Usage:    "登录 login [nacos-url]->http",
			Category: "Basic Commands (Intermediate)",
			Action:   loginHandler,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name: "namespace, n",
					// Required: true,
					Value: "public",
					Usage: "指定命名空间",
				},
				cli.StringFlag{
					Name:  "prefix, pf",
					Value: "/nacos",
					Usage: "指定nacos的prefix",
				},
				cli.Uint64Flag{
					Name:  "port, p",
					Value: 80,
					Usage: "指定nacos的端口",
				},
			},
		},
		{
			Name:     "logout",
			Usage:    "退出",
			Category: "Basic Commands (Intermediate)",
			Action:   logoutHandler,
		},
	}

	sort.Sort(cli.FlagsByName(app.Flags))

	cli.HelpFlag = cli.BoolFlag{
		Name:  "help, h",
		Usage: "查看帮助信息",
	}

	cli.VersionFlag = cli.BoolFlag{
		Name:  "version, v",
		Usage: "查看版本信息",
	}

	app.Run(os.Args)
}
